package cn.wolfcode.p2p.website.web.controller;

import cn.wolfcode.p2p.base.anno.RequiredLogin;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.ISystemDictionaryItemService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.base.util.LoginInfoContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    // 跳转我要贷款页
    @RequestMapping("/borrow")
    public String toBorrow(Model model){
        // 查询是否已经登录
        LoginInfo loginInfo = LoginInfoContext.getCurrentLoginInfo();
        if (loginInfo == null){
            return "redirect:/borrow.html";
        }
        model.addAttribute("userInfo", userInfoService.get(loginInfo.getId()));
        model.addAttribute("account", accountService.get(loginInfo.getId()));
        return "borrow";
    }

    // 修改个人信息
    @RequestMapping("/basicInfo")
    @RequiredLogin
    public String toBasicInfo(Model model){

        model.addAttribute("userInfo", userInfoService.get(LoginInfoContext.getCurrentLoginInfo().getId()));

        model.addAttribute("educationBackgrounds", systemDictionaryItemService.listItemByDicSn("educationBackground"));
        model.addAttribute("incomeGrades", systemDictionaryItemService.listItemByDicSn("incomeGrade"));
        model.addAttribute("kidCounts", systemDictionaryItemService.listItemByDicSn("kidCount"));
        model.addAttribute("houseConditions", systemDictionaryItemService.listItemByDicSn("houseCondition"));
        model.addAttribute("educationBackgrounds", systemDictionaryItemService.listItemByDicSn("educationBackground"));
        model.addAttribute("marriages", systemDictionaryItemService.listItemByDicSn("marriage"));

        return "userInfo";
    }

    @RequestMapping("/basicInfo_save")
    @ResponseBody
    public JSONResult saveBasicInfo(UserInfo vo){

        userInfoService.saveUserInfo(vo);

        return new JSONResult();
    }
}
